<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/15
 * Time: 19:50
 */

namespace app\api\controller;

use app\api\model\Help as HelpModel;
use app\api\model\ReceiveTask as ReceiveModel;
use app\api\model\Task as TaskModel;
use app\api\service\User as UserService;
use think\Db;

class Sys
{
    //帮助列表
    public function helpList()
    {
        $list = HelpModel::order('add_time desc')->all();
        return json(['errorCode' => 0, 'data' => $list]);
    }

    //上传投诉
    public function addComplaint()
    {
        $user_id = UserService::getUserIdByToken();
        $task_no = input('post.task_no');
        $task = TaskModel::get(['number' => $task_no]);
        $receive = ReceiveModel::get(['task_id'=>$task['id'],'user_id'=>$user_id]);
        if (!$receive) {
            return json(['errorCode' => 1, 'msg' => '任务编号有误']);
        }
        // 获取表单上传文件
        $image1 = request()->file('image1');
        $image2 = request()->file('image2');
        if (!$image1){
            return json(['errorCode' => 1, 'msg' => '没有上传图片']);
        }
        //图片上传
        $dir = DS . 'uploads' . DS . 'wechat' . DS . 'qrcode' . DS . date('Ym');
        $save = ROOT_PATH . 'public' . $dir;
        $info = $image1->validate(['ext'=>'jpg,png'])->rule('uniqid')->move($save);
        if ($info) {
            // 成功上传后 获取上传信息
            $images[] = $dir . DS . $info->getSaveName();
        } else {
            // 上传失败获取错误信息
//                echo $file->getError();
//                return json(['errorCode'=>1,'msg'=>'图片上传失败']);
            return json(['errorCode' => 1, 'msg' => $image1->getError()]);
        }
        if ($image2){
            //图片上传
            $dir = DS . 'uploads' . DS . 'wechat' . DS . 'qrcode' . DS . date('Ym');
            $save = ROOT_PATH . 'public' . $dir;
            $info = $image2->validate(['ext'=>'jpg,png'])->rule('uniqid')->move($save);
            if ($info) {
                // 成功上传后 获取上传信息
                $images[] = $dir . DS . $info->getSaveName();
            } else {
                // 上传失败获取错误信息
//                echo $file->getError();
//                return json(['errorCode'=>1,'msg'=>'图片上传失败']);
                return json(['errorCode' => 1, 'msg' => $image2->getError()]);
            }
        }
        /*if (empty($files)) {
            return json(['errorCode' => 1, 'msg' => '没有上传图片']);
        }
        foreach ($files as $file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $dir = DS . 'uploads' . DS . 'complaint' . DS . date('Ym');
            $save = ROOT_PATH . 'public' . $dir;
            $info = $file->rule('uniqid')->move($save);
            if ($info) {
                // 成功上传后 获取上传信息
//                $filename = $dir . DS . $info->getSaveName();
                $images[] = $dir . DS . $info->getSaveName();
            } else {
                // 上传失败获取错误信息
//                echo $file->getError();
//                return json(['errorCode'=>1,'msg'=>'图片上传失败']);
                return json(['errorCode' => 1, 'msg' => $file->getError()]);
            }
        }*/
        //保存到投诉记录
        $c_data = [
            'user_id' => $user_id,
            'task_id' => $task['id'],
            'task_no' => $task_no,
            'img' => json_encode($images, JSON_UNESCAPED_UNICODE),
            'status' => 0,
            'add_time' => time()
        ];
        $res = Db::name('complaint')->insert($c_data);
        if (!$res) {
            return json(['errorCode' => 1, 'msg' => '投诉失败']);
        }
        return json(['errorCode' => 0, 'msg' => '投诉成功']);
    }

    //添加设备号
    public function addDevice(){
        $jpush_reg_id = input('post.jpush_reg_id');
        $device_no = input('post.device_no');
        $device_type = input('post.device_type');
        $device = Db::name('device_jpush');
        $info = $device->where('device_no',$device_no)->find();
        if (!$info){
            $data = [
                'device_no' => $device_no,
                'jpush_reg_id' => $jpush_reg_id,
                'device_type' => $device_type,
            ];
            $device->insert($data);
        }
    }
}